package com.demo.accessibility.floatwindows

import android.util.Log
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.widget.TextView
import com.demo.accessibility.R
import com.demo.accessibility.base.BaseFloatWindowManager
import com.demo.accessibility.service.NurseHelperService
import com.demo.accessibility.utils.AccessibilityUtils

/**
 * ================================================
 * 抓取微信个人界面的信息
 * Created by ligang on 2024/4/22
 * ================================================
 */
class GatherWechatInfoFloatWindowManager : BaseFloatWindowManager() {
    companion object {
        private const val TAG = "GatherWechatInfoFloatWindowManager"
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { GatherWechatInfoFloatWindowManager() }
    }

    override fun provideFloatView() = R.layout.view_gather_wechat_info

    private fun rootInActiveWindow() = NurseHelperService.instance?.rootInActiveWindow

    private var mInfoText: TextView? = null
    override fun initView(floatView: View) {
        mInfoText = floatView.findViewById(R.id.tv_info)
        floatView.findViewById<View>(R.id.btn_back_app).setOnClickListener {
            AccessibilityUtils.returnApp()
            remove()
        }
        floatView.findViewById<View>(R.id.btn_gather).setOnClickListener {
            // 手动触发采集页面信息
            gatherInfo()
        }
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        event?.let {
            val eventType = event.eventType
            if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                val packageName = event.packageName?.toString()
                val className = event.className?.toString()

                Log.e("============", "packageName：${packageName},className: $className")
                // 检查当前活动窗口的包名和类名是否符合你要采集信息的 Activity
                if (packageName == "com.tencent.mm" && className == "com.tencent.mm.plugin.profile.ui.ContactInfoUI") {
                    // 在这里进行自动采集

                }
            }
        }
    }

    private fun gatherInfo() {
        rootInActiveWindow()?.let { rootNode ->
            val sb = StringBuffer()
            // 获取备注
            AccessibilityUtils.findNoteInfoById(rootNode, "com.tencent.mm:id/cf8")?.let {
                Log.e(TAG, "${it.text}")
                sb.append("${it.text}\n")
            }
            // 获取昵称
            AccessibilityUtils.findNoteInfoById(rootNode, "com.tencent.mm:id/cf7")?.let {
                Log.e(TAG, "${it.text}")
                sb.append("${it.text}\n")
            }
            // 获取微信号
            AccessibilityUtils.findNoteInfoById(rootNode, "com.tencent.mm:id/cff")?.let {
                Log.e(TAG, "${it.text}")
                sb.append("${it.text}\n")
            }
            // 获取地区
            AccessibilityUtils.findNoteInfoById(rootNode, "com.tencent.mm:id/cf6")?.let {
                Log.e(TAG, "${it.text}")
                sb.append("${it.text}")
            }
            mInfoText?.text = sb
        }
    }
}